/**
 * Created by niexiaosong on 18/7/13.
 */

var jgbm = "";

$(document).ready(function () {
    
    var jgLoading = {"name" : "加载中..."};
    $.fn.zTree.init($("#tree_jg"), setting, jgLoading);
    getJgTree();
    fuzzySearch('tree_jg', '#key', null, true); //初始化模糊搜索方法
    //当前机构
    getJgbm();
    //机构权限
    getZcjgQx();


});

//**********************左侧树形开始
var setting_jgqx = {
    check: {
        enable: true
    },
    data: {
        simpleData: {
            enable: true
        }
    }
};


var setting = {
    view: {
        addHoverDom: addHoverDom,
        removeHoverDom: removeHoverDom,
        selectedMulti: false,
        showIcon:false

    },
    data: {
        simpleData: {
            enable: true
        }
    },
    callback: {
        onClick: jgTreeOnClick,
        onExpand: zTreeOnExpand
    }
};

var zNodes_jg = [{}];

//***********************左侧树形end
//****************机构权限设置 树形 start
var setting_jgqx = {
    check: {
        enable: true
    },
    data: {
        simpleData: {
            enable: true
        }
    },
    view: {
        showIcon: false
    }
};

var zNodes_jgqx = [];
//****************机构权限设置 树形end

//***************************ztree 点击样式 *************
var newCount = 1;
function addHoverDom(treeId, treeNode) {
    var sObj = $("#" + treeNode.tId + "_span");
    if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0) return;
    var addStr = "<span class='button add' id='addBtn_" + treeNode.tId + "' title='add node' onfocus='this.blur();'></span>";
    sObj.after(addStr);
    var btn = $("#addBtn_" + treeNode.tId);
    if (btn) btn.bind("click",
        function () {
            var zTree = $.fn.zTree.getZTreeObj("tree_jg");
            zTree.addNodes(treeNode, {
                id: (100 + newCount),
                pId: treeNode.id,
                name: "new node" + (newCount++)
            });
            return false;
        });
}
;
function removeHoverDom(treeId, treeNode) {
    $("#addBtn_" + treeNode.tId).unbind().remove();
}
;

//***************************ztree 点击样式 *************


/**
 * 定义树节点展开事件，查询子节点
 * @param event
 * @param treeId
 * @param treeNode
 */
function zTreeOnExpand(event, treeId, treeNode){
    queryChildrenList(treeNode);
}

/**
 * 查询子节点
 * @param treeNode
 */
function queryChildrenList(treeNode){
    var jgmc = $("#key").val();
    if(jgmc != '' || treeNode.children != undefined){
        return;
    }
    var queryParms = {
        pid : treeNode.id,
        jgmc : jgmc
    };
    $.ajax({
        type: "GET",
        contentType: 'application/json',
        dataType: "json",
        url: "commonapi/findjgtreeByPid",
        data: queryParms,
        success:function(data){
            //添加子节点到指定的父节点
            var jsondata= eval(data.jgTreeList);
            for(var i=0;i<jsondata.length;i++){
                if(jsondata[i].isLeaf != '1'){
                    jsondata[i].isParent = true;
                }
            }
            if(jsondata != null && jsondata != ""){
                var treeObj = $.fn.zTree.getZTreeObj("tree_jg");
                var parentZNode = treeObj.getNodeByParam("id", treeNode.id, null);
                treeObj.addNodes(parentZNode,jsondata, false);
            }
        },
        error: function () {
            alert('请求失败');
        }
    });
}



/**
 * 获取当前机构树形
 */
function getJgTree() {
    $.ajax({
        type: "GET",
        contentType: 'application/json',
        dataType: "json",
        url: "commonapi/findjgtree",
        success: function (jsonData) {
            if (jsonData.code == 200) {
                var jgTreeList = jsonData.jgTreeList;
                for(var i=0;i<jgTreeList.length;i++){
                    if(jgTreeList[i].isLeaf != '1'){
                        jgTreeList[i].isParent = true;
                    }
                }

                $.fn.zTree.init($("#tree_jg"), setting, jsonData.jgTreeList);
                var treeObj = $.fn.zTree.getZTreeObj("tree_jg");
                var nodes = treeObj.getNodes();

                if (nodes.length>0) {
                   /* //默认展开二级节点
                    if(nodes[0].children.length>0){
                        nodes=nodes[0].children;
                    }*/
                    treeObj.expandNode(nodes[0], true, false, true);
                    //默认选中根节点
                    treeObj.selectNode(treeObj.getNodes()[0]);
                    getCheckZcjgQx(treeObj.getNodes()[0].id);
                }

            } else {
            }
        }
    });
}


/**
 * 机构树形选中
 */

function jgTreeOnClick(event, treeId, treeNode, clickFlag) {
    //重新渲染机构权限树
    getCheckZcjgQx(treeNode.id);
   // var treeObj = $.fn.zTree.getZTreeObj("tree_jgqx");
   // treeObj.cancelSelectedNode();
    jgbm = treeNode.id;
}

/**
 * 获取当前机构编码
 */
function getJgbm() {
    $.ajax({
        type: "GET",
        contentType: 'application/json',
        dataType: "json",
        url: "commonapi/jgbm",
        success: function (jsonData) {
            if (jsonData.code == 200) {
                jgbm = jsonData.jgbm;
            }
        }
    });
}

 // 获取当前机构权限
 function getZcjgQx()
 {
     $.ajax({
         type: "GET",
         contentType: 'application/json',
         dataType: "json",
         url: "role/getzcjgqx",
         success: function (jsonData) {
             if (jsonData.code == 200) {
                 $.fn.zTree.init($("#tree_jgqx"), setting_jgqx, jsonData.sysGncdList);
             }
         }
     });
 }



 //复选样式
  function showCheckCss()
  {

      $('.i-checks').iCheck({
          checkboxClass: 'icheckbox_square-green',
          radioClass: 'iradio_square-green',
      });
  }
//清空复选框
function selectNull(){
    $("input[name='jgqxname']").each(function(){
        this.checked = false;
    });
    showCheckCss();
}

  //根据选中机构编码 获取对应机构权限并选中
    function getJgqxByJgbm(jgbm)
    {
        selectNull();
        $.ajax({
            type: "GET",
            contentType: 'application/json',
            dataType: "json",
            url: "role//getcheckzcjgqx/"+jgbm,
            success: function (jsonData) {
                if (jsonData.code == 200) {
                    //选中集合
                    var list = jsonData.sysGncdList;
                    var treeObj = $.fn.zTree.getZTreeObj("tree_jgqx");
                    if(list.length >0) {
                        for (var i = 0; i < list.length; i++) {
                            var data = list[i];
                            var node = treeObj.getNodeByParam("id",data.cddm);
                            if(node)
                                treeObj.checkNode(node, true, false);
                        }
                    }

                    // var treeObj = $.fn.zTree.getZTreeObj("tree_jgqx");
                    // var nodes = treeObj.getNodes();
                    //
                    // if (nodes.length>0) {
                    //     //默认展开一级节点
                    //     treeObj.expandNode(nodes[0], true, false, true);
                    // }

                }
                showCheckCss();
            }
        });

    }

   //保存机构权限
   function saveJgqx()
   {

       var treeObj = $.fn.zTree.getZTreeObj("tree_jgqx");
       var nodes = treeObj.getCheckedNodes(true);
       //console.log(nodes);
       var check_val = "";
       if(nodes.length>0)
       {
           for(var i=0;i<nodes.length;i++){
               check_val+=nodes[i].id + ",";
           }
       }

       $("#btnSave").attr('disabled',true);
       $.ajax({
           type: "POST",
           contentType: 'application/x-www-form-urlencoded',
           dataType: "json",
           url: "role/savejgqx",
           data:
          {
               jgqxCheckList:check_val,
               jgbm:jgbm
           },
           success: function (jsonData) {
               $("#btnSave").removeAttr('disabled');
               if (jsonData.code == 200) {   //查询成功
                   LayerWn.showSucAlert("保存成功");
               }else if(jsonData.code == 201){
                   LayerWn.showSucAlert(jsonData.msg);
               }
           }
       });

   }

//提交前校验
function validate(check_val){

    if(check_val.length==0)
    {
        LayerWn.showAlert('请选机构权限!');
        return false;
    }
    return true;
}

//机构权限复制
function copyJgqx()
{
    //校验所选机构是否具有权限
    $.ajax({
        type: "GET",
        contentType: 'application/json',
        dataType: "json",
        url: "role/getcheckzcjgqx/" + jgbm,
        success: function (jsonData) {
            if (jsonData.code == 200) {
                var list = jsonData.sysGncdList;
                if(list.length==0){
                    LayerWn.showAlert('所选机构无权限!');
                    return;
                }
                else {
                    LayerWn.openDialog('机构权限复制', 'plat-copy-jgqx.html?jgbm=' + jgbm, 700, 500);
                }
            }
        }
    });



}


// 获取选中的机构权限
function getCheckZcjgQx(check_jgbm)
{
    $.ajax({
        type: "GET",
        contentType: 'application/json',
        dataType: "json",
        url: "role/getzcjgqx",
        success: function (jsonData) {
            if (jsonData.code == 200) {
                $.fn.zTree.init($("#tree_jgqx"), setting_jgqx, jsonData.sysGncdList);
                getJgqxByJgbm(check_jgbm);
            }
        }
    });
}
